<template>
  <div id="my_form">
    <el-form
      class="form"
      ref="form_DOM"
      :model="form_Data"
      :rules="rules"
      :validate-on-rule-change="false"
      v-bind="config?.form_config"
    >
      <el-row v-bind="config?.row_config">
        <el-col :span="4" :offset="1" v-bind="item?.col" v-for="item in config?.column" :key="item.name">
          <el-form-item
            :class="'my_form_item_' + item.name"
            v-bind="item?.form_item_config"
            v-if="item?.type != 'img' && item?.type != 'text' && item?.type != 'slot'"
          >
            <template #label v-if="item?.isLabel === true">
              <slot :name="item.name + 'Label'"></slot>
            </template>
            <!-- 输入框 - 单行 -->
            <el-input
              class="my-item my-input"
              v-model="form_Data[item.name]"
              clearable
              v-bind="item?.content_item_config"
              v-if="item?.type === 'input'"
              @change="item?.onChange"
            />
            <!-- 输入框 - 多行 -->
            <el-input
              class="my-item my-input"
              v-model="form_Data[item.name]"
              type="textarea"
              maxlength="100"
              show-word-limit
              v-bind="item?.content_item_config"
              v-if="item?.type === 'textarea'"
              @change="item?.onChange"
            />
            <!-- 多选框 -->
            <el-select
              class="my-item my-select"
              v-model="form_Data[item.name]"
              clearable
              v-bind="item?.content_item_config"
              v-if="item?.type === 'select'"
              @change="item?.onChange"
            >
              <el-option v-for="i in item.options" :key="i.value" :label="i.label" :value="i.value" />
            </el-select>
            <!-- 级联选择器 -->
            <el-cascader
              class="my-item my-cascader"
              v-model="form_Data[item.name]"
              clearable
              v-bind="item?.content_item_config"
              :options="item.options"
              v-if="item?.type === 'cascader'"
              @change="item?.onChange"
            />
            <!-- 时间 -->
            <el-time-picker
              class="my-item my-timePicker"
              v-model="form_Data[item.name]"
              is-range
              range-separator="至"
              v-bind="item?.content_item_config"
              @change="item?.onChange"
              v-if="item?.type === 'timePicker'"
            />
            <!-- 日期 -->
            <el-date-picker
              class="my-item my-datePicker"
              v-model="form_Data[item.name]"
              type="date"
              clearable
              v-bind="item?.content_item_config"
              @change="item?.onChange"
              v-if="item?.type === 'datePicker'"
            />
            <!-- 日期 -->
            <el-date-picker
              class="my-item my-datetimerange"
              v-model="form_Data[item.name]"
              type="datetimerange"
              range-separator="To"
              start-placeholder="Start date"
              end-placeholder="End date"
              clearable
              v-bind="item?.content_item_config"
              @change="item?.onChange"
              v-if="item?.type === 'datetimerange'"
            />
          </el-form-item>
          <el-form-item
            :class="'my-form-item my_form_item_' + item.name"
            v-bind="item?.form_item_config"
            v-if="item?.type === 'img' || item?.type === 'text' || item?.type === 'slot'"
          >
            <template #label v-if="item?.isLabel === true">
              <slot :name="item?.name + 'Label'"></slot>
            </template>
            <!-- 插槽 -->
            <slot :name="item.name" v-if="item?.type === 'slot'"></slot>
            <!-- 文字 -->
            <div class="my-item my-text" v-if="item?.type === 'text'">
              {{ form_Data[item.name] ? form_Data[item.name] : '---' }}
            </div>
            <!-- 图片 -->
            <div class="img_box" v-if="item?.type === 'img'">
              <el-image
                class="img"
                :src="i"
                v-for="i in form_Data[item.name]?.split(',')"
                :key="i"
                :zoom-rate="1.2"
                :max-scale="7"
                :min-scale="0.2"
                :initial-index="4"
                :preview-src-list="form_Data[item.name]?.split(',')"
                fit="cover"
              />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, watch, nextTick } from 'vue'
import type { FormInstance } from 'element-plus'
// ========================== 实例

const form_DOM = ref<FormInstance>()

// ========================== 数据
const form_Data = reactive<any>({})

// ========================== 钩子

const props = defineProps<{
  config: any
}>()

onMounted(() => {
  props.config?.column.map((item: any) => {
    if (item.type != 'slot') {
      form_Data[item.name] = undefined
    }
  })
  isRules(props.config?.column)
})

watch(
  () => props.config,
  (val: any) => {
    console.log('监听：', val)
    resetForm(form_DOM.value)
    isRules(props.config?.column)
  },
  {
    deep: true
  }
)
// ========================== 函数
// 是否校验
const isRules = (data: any) => {
  data.map((item: any) => {
    if (item.type != 'slot' && item.type != 'text') {
      if (item.form_item_config.prop && item.rules == undefined) {
        rules[item.form_item_config.prop] = [{ required: false, validator: validate, trigger: 'blur' }]
        // console.log(item)
      } else if (item.rules != undefined) {
        rules[item.form_item_config.prop] = [{ required: false, validator: item.rules, trigger: 'blur' }]
        // console.log(item)
      }
    }
  })
}

// 非空校验
const validate = (rule: any, value: any, callback: any) => {
  if (value === null) {
    return callback(new Error('内容不能为空'))
  }

  if (value === undefined) {
    return callback(new Error('内容不能为空'))
  }

  if ((value + '').match(/^[ ]*$/)) {
    return callback(new Error('内容不能为空'))
  }

  return callback()
}

const rules = reactive<any>({}) // 校验名单

// 提交
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  return await formEl.validate((valid: any, fields: any): any => {
    if (valid) {
      return true
    } else {
      console.log('error submit!', fields)
      return false
    }
  })
}

// 取消
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.clearValidate(Object.keys(form_Data))
}

// 暴露
defineExpose({ form_DOM, form_Data, submitForm })
</script>

<style lang="less" scoped>
#my_form {
  width: 100%;
}

:deep(.my-item) {
  // height: 30px !important;
  // min-height: 30px !important;
}

// :deep(.my-input) {
//   width: 200px !important;
// }

:deep(.my-text) {
  white-space: normal;
  word-break: break-all;
  word-wrap: break-word;
  color: #000;
}

:deep(.img_box) {
  .img {
    margin-right: 10px;
  }
}
.img {
  .width_height(95px,95px);
}

.form {
  :deep(.el-row) {
    .el-col {
      .el-form-item {
        margin-bottom: 0px;
        .el-form-item__label {
          color: #000;
        }
      }
    }
    .el-form-item {
      margin-bottom: 0px;
    }
    .my-form-item {
      // padding: 0px !important;
      // margin: 12px 0;
      .el-form-item__label {
        // height: auto;
        // line-height: normal;
      }
      .el-form-item__content {
        // line-height: normal;
      }
    }
  }
}
</style>
